package com.zjw.forum.services.impl;

import com.zjw.forum.common.AppResult;
import com.zjw.forum.common.ResultCode;
import com.zjw.forum.dao.ArticleMapper;
import com.zjw.forum.dao.BoardMapper;
import com.zjw.forum.dao.UserMapper;
import com.zjw.forum.exception.ApplicationException;
import com.zjw.forum.model.Article;
import com.zjw.forum.model.Board;
import com.zjw.forum.model.User;
import com.zjw.forum.services.IArticleService;
import com.zjw.forum.services.IBoardService;
import com.zjw.forum.services.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ArticleServiceImpl implements IArticleService {

    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private IBoardService boardService;
    @Autowired
    private IUserService userService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private BoardMapper boardMapper;

    @Override
    public void postArticle(Article article) {
        //校验插入时的必传属性
        if(article==null||article.getBoardId()==null
        ||article.getUserId()==null||article.getContent()==null
        ||article.getTitle()==null){
            log.warn(ResultCode.FAILED_NOT_EXISTS.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_NOT_EXISTS));
        }
        //插入文章信息之前给默认值初始化
        article.setDeleteState((byte)0);
        article.setState((byte)0);
        article.setLikeCount(0);
        article.setReplyCount(0);
        article.setVisitCount(0);
        Date date=new Date();
        article.setCreateTime(date);
        article.setUpdateTime(date);
        //插入数据
        int i = articleMapper.insertSelective(article);
        if(i!=1){
            log.warn(ResultCode.ERROR_SERVICES.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        //给用户更新articlecount
        int tmp = userService.addArticleCount(article.getUserId());
        if(tmp!=1){
            log.warn(ResultCode.ERROR_SERVICES.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        //给板块更新articlecount
        int tmp1 = boardService.addArticleCount(article.getBoardId());
        if(tmp1!=1){
            log.warn(ResultCode.ERROR_SERVICES.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        log.info("成功发布帖子 user id="+article.getUserId()+", board Id="+article.getBoardId());

    }

    @Override
    public List<Article> selectAllByBoardId(Long boardId) {
        //参数校验
        if(boardId==null||boardId<=0){
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        //查询boardId是否合法
        Board board = boardService.getBoardInfoById(boardId);
        if(board==null){
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        //查询文章
        List<Article> articles = articleMapper.selectByBoardId(boardId);
        return articles;
    }

    @Override
    public Article selectDetailById(Long id) {
        if(id==null||id<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //查询文章是否存在
        Article article = articleMapper.selectDetailById(id);
        if(article==null){
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        //浏览量加1
        //构造修改的对象，必传文章id，及要修改的属性
        Article article1=new Article();
        article1.setId(article.getId());
        article1.setVisitCount(article.getVisitCount()+1);
        int i = articleMapper.updateByPrimaryKeySelective(article1);
        if(i!=1){
            log.warn(ResultCode.FAILED.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
        //由于更新的时候，article已经生成了，所以要给article也更新一下
        article.setVisitCount(article.getVisitCount()+1);
        //返回文章详情
        return article;
    }

    @Override
    public int updateArticleById(Long id, String title, String content) {
        //参数校验
        if(id==null||id<=0|| !StringUtils.hasLength(title) ||!StringUtils.hasLength(content)){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //更新
        Article article=new Article();
        article.setId(id);
        article.setTitle(title);
        article.setContent(content);
        return articleMapper.updateByPrimaryKeySelective(article);
    }

    @Override
    public Article selectArticle(Long id) {
        //参数校验
        if(id==null||id<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //调用mapper
        Article article = articleMapper.selectById(id);
        if(article==null){
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        return article;
    }

    @Override
    public int updateLikeCount(Long id) {
        //参数校验
        if(id==null||id<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //校验文章是否存在
        Article article =articleMapper.selectById(id);
        if(article==null||article.getDeleteState()==1){
            log.error(ResultCode.FAILED_ARTICLE_NOT_EXISTS.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        //校验文章是否被禁用
        if(article.getState()==1){
            log.error(ResultCode.FAILED_ARTICLE_BANNED.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED));
        }
        //构造
        Article article1=new Article();
        article1.setId(id);
        article1.setVisitCount(article.getVisitCount()+1);
        return articleMapper.updateByPrimaryKeySelective(article);
    }

    @Override
    public void deleteAritcle(Long id) {
        //参数校验
        if(id==null||id<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //校验帖子是否正常
        Article article = articleMapper.selectById(id);
        if(article==null||article.getDeleteState()==1){
            log.error(ResultCode.FAILED_ARTICLE_NOT_EXISTS.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        //校验文章是否被禁用
        if(article.getState()==1){
            log.error(ResultCode.FAILED_ARTICLE_BANNED.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED));
        }
        //校验用户是否正常
        User user = userMapper.selectByPrimaryKey(article.getUserId());
        if(user==null){
            log.error(ResultCode.FAILED_USER_NOT_EXISTS.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        if(user.getState()==1){
            log.error(ResultCode.FAILED_USER_BANNED.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_BANNED));
        }
        //开始更新文章的deleteState
        Article article1=new Article();
        article1.setId(id);
        article1.setDeleteState((byte)1);
        int i = articleMapper.updateByPrimaryKeySelective(article1);
        if(i!=1){
            log.warn("文章删除失败");
            throw new ApplicationException(AppResult.failed("文章删除失败"));
        }
        //给用户更新articlecount
        int tmp = userService.subArticleCount(article.getUserId());
        if(tmp!=1){
            log.warn("用户更新失败");
            throw new ApplicationException(AppResult.failed("用户更新失败"));
        }
        //给板块更新articlecount
        int tmp1 = boardService.subArticleCount(article.getBoardId());
        if(tmp1!=1){
            log.warn("板块更新失败");
            throw new ApplicationException(AppResult.failed("板块更新失败"));
        }
        log.info("成功删除帖子 user id="+article.getUserId()+", board Id="+article.getBoardId());
    }

    @Override
    public int addReplayCount(Long id) {
        if(id==null||id<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //校验帖子是否正常
        Article article = articleMapper.selectById(id);
        if(article==null||article.getDeleteState()==1){
            log.error(ResultCode.FAILED_ARTICLE_NOT_EXISTS.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        //校验文章是否被禁用
        if(article.getState()==1){
            log.error(ResultCode.FAILED_ARTICLE_BANNED.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED));
        }
        //构造对象更新回复数
        Article article1=new Article();
        article1.setId(id);
        article1.setReplyCount(article.getReplyCount()+1);
        return articleMapper.updateByPrimaryKeySelective(article1);
    }

    @Override
    public List<Article> selectAllByUserId(Long userId) {
        //参数校验
        if(userId==null||userId<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //查询user是否存在
        User user = userMapper.selectByPrimaryKey(userId);
        if(user==null){
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        //根据userId查询所有的article
        return articleMapper.selectAllByUserId(userId);
    }
}
